package cn.dxs.gitee.ui.activity.base;

import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.CallSuper;
import android.support.annotation.IdRes;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.Display;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;

import java.util.List;

import butterknife.ButterKnife;
import cn.dxs.gitee.App;
import cn.dxs.gitee.AppData;
import cn.dxs.gitee.mvp.contract.base.IBaseContract;
import cn.dxs.gitee.ui.activity.LoginActivity;
import cn.dxs.gitee.ui.activity.SplashActivity;
import cn.dxs.gitee.util.AppUtils;
import cn.dxs.gitee.util.PrefUtils;
import cn.dxs.gitee.util.ThemeHelper;
import cn.dxs.gitee.util.WindowUtil;
import es.dmoral.toasty.Toasty;

/**
 * Activity基类
 *
 * @author lijian
 * @date 2019-09-20 23:10
 */
public abstract class BaseActivity<P extends IBaseContract.Presenter> extends AppCompatActivity implements IBaseContract.View {

    protected P mPresenter;
    private ProgressDialog mProgressDialog;
    private static BaseActivity curActivity;

    protected boolean isAlive = true;// 是存活的
//    @BindView(R.id.toolbar)
//    @Nullable
//    protected Toolbar toolbar;
//    @BindView(R.id.toolbar_layout)
//    @Nullable
//    protected CollapsingToolbarLayout toolbarLayout;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        // 如果未登录并且当前不是SplashActivity，LoginActivity页面，则跳转至登录页面
        if ((AppData.INSTANCE.getAuthUser() == null || AppData.INSTANCE.getLoggedUser() == null)
                && !this.getClass().equals(SplashActivity.class)
                && !this.getClass().equals(LoginActivity.class)) {
            super.onCreate(savedInstanceState);
            finishAffinity();
            startActivity(new Intent(getActivity(), SplashActivity.class));
            return;
        }

        // 应用主题
        ThemeHelper.apply(this);
        // 更新APP语言
        AppUtils.updateAppLanguage(getActivity());
        super.onCreate(savedInstanceState);
        isAlive = true;// 标记当前activity是存活的

        // MVP绑定
        if (mPresenter != null) {
            // 恢复实例状态
            mPresenter.onRestoreInstanceState(savedInstanceState == null ? getIntent().getExtras() : savedInstanceState);
            // 绑定View
            mPresenter.attachView(this);
        }

        // 获取屏幕宽高
        getScreenSize();

        // 设置视图布局，并注册组件自动查找attachView
        if (getContentView() != 0) {
            setContentView(getContentView());
            ButterKnife.bind(getActivity());
        }

        // 初始化activity（一般处理数据初始化）
        initActivity();

        // 初始化视图
        initView(savedInstanceState);

        // view已初始化，可以初始化view数据
        if (mPresenter != null) mPresenter.onViewInitialized();

        // 是否全屏
        if (isFullScreen) {
            intoFullScreen();
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        //系统由于内存不足而杀死activity，此时保存数据
//        DataAutoAccess.saveData(this, outState);
        if (mPresenter != null) mPresenter.onSaveInstanceState(outState);
//        if (curActivity.equals(this)) {
//            DataAutoAccess.saveData(AppData.INSTANCE, outState);
//        }
    }

    /**
     * 给activity设置布局
     *
     * @return
     */
    @LayoutRes
    protected abstract int getContentView();

    /**
     * 初始化activity
     */
    @CallSuper
    protected void initActivity() {

    }

    /**
     * 初始化view（默认实现了toolbar的双击事件）
     */
    @CallSuper
    protected void initView(Bundle savedInstanceState) {
//        if (toolbar != null) {
//            setSupportActionBar(toolbar);
//            DoubleClickHandler.setDoubleClickListener(toolbar, new DoubleClickHandler.DoubleClickListener() {
//                @Override
//                public void onDoubleClick(View view) {
//                    onToolbarDoubleClick();
//                }
//            });
//        }
    }

    protected void onToolbarDoubleClick() {
        PrefUtils.set(PrefUtils.DOUBLE_CLICK_TITLE_TIP_ABLE, false);
    }

    @Override
    protected void onResume() {
        super.onResume();
        curActivity = getActivity();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) mPresenter.detachView();
        if (this.equals(curActivity)) curActivity = null;
        isAlive = false;
    }

    protected void setToolbarBackEnable() {
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }
    }

    protected void setToolbarTitle(String title) {
        if (getSupportActionBar() != null) {
            getSupportActionBar().setTitle(title);
        }
//        if (toolbarLayout != null) {
//            toolbarLayout.setTitle(title);
//        }
    }

    protected void setToolbarTitle(String title, String subTitle) {
        setToolbarTitle(title);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setSubtitle(subTitle);
        }
    }

    protected void setToolbarSubTitle(String subTitle) {
        if (getSupportActionBar() != null) {
            getSupportActionBar().setSubtitle(subTitle);
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finishActivity();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public void finishActivity() {
        finish();
    }

    protected Fragment getVisibleFragment() {
        @SuppressLint("RestrictedApi")
        List<Fragment> fragmentList = getSupportFragmentManager().getFragments();
        if (fragmentList != null) {
            for (Fragment fragment : fragmentList) {
                if (fragment != null && fragment.isVisible()) {
                    return fragment;
                }
            }
        }
        return null;
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showProgressDialog(String content) {
        getProgressDialog(content);
        mProgressDialog.show();
    }

    @Override
    public void dismissProgressDialog() {
        if (mProgressDialog != null) {
            mProgressDialog.dismiss();
        } else {
            throw new NullPointerException("dismissProgressDialog: can't dismiss a null dialog, must showForRepo dialog first!");
        }
    }

    @Override
    public ProgressDialog getProgressDialog(String content) {
        if (mProgressDialog == null) {
            mProgressDialog = new ProgressDialog(getActivity());
            mProgressDialog.setCancelable(false);
        }
        mProgressDialog.setMessage(content);
        return mProgressDialog;
    }

    @Override
    public void showToast(String message) {
        Toasty.normal(getActivity(), message).show();
    }

    @Override
    public void showInfoToast(String message) {
        Toasty.info(getActivity(), message).show();
    }

    @Override
    public void showSuccessToast(String message) {
        Toasty.success(getActivity(), message).show();
    }

    @Override
    public void showErrorToast(String message) {
        Toasty.error(getActivity(), message).show();
    }

    @Override
    public void showWarningToast(String message) {
        Toasty.warning(getActivity(), message).show();
    }

    @Override
    public void showTipDialog(String msg) {
        new AlertDialog.Builder(getActivity())
                .setCancelable(true)
                .setTitle("提示")
                .setMessage(msg)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(@NonNull DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                })
                .show();
    }

    @Override
    public void showConfirmDialog(String msg, String title, String confirmText, DialogInterface.OnClickListener confirmListener) {
        new AlertDialog.Builder(getActivity())
                .setCancelable(true)
                .setTitle(title)
                .setMessage(msg)
                .setCancelable(true)
                .setPositiveButton(confirmText, confirmListener)
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(@NonNull DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                })
                .show();
    }

    public static BaseActivity getCurActivity() {
        return curActivity;
    }

    @NonNull
    protected BaseActivity getActivity() {
        return this;
    }

    @NonNull
    protected App getAppApplication() {
        return (App) getApplication();
    }

    public void onRefreshWebPage() {

    }

    /**
     * @param id  viewId
     * @param <T> View
     * @return View
     * @Description: 优化activity的方法，添加类型自动转换
     * @author: Yuyunhao
     */
    @Nullable
    protected <T extends View> T findViewByViewId(@IdRes int id) {
        return (T) findViewById(id);
    }

    private void getScreenSize() {
        if (WindowUtil.screenHeight == 0 ||
                WindowUtil.screenWidth == 0) {
            Display display = getWindowManager().getDefaultDisplay();
            WindowUtil.screenWidth = display.getWidth();
            WindowUtil.screenHeight = display.getHeight();
        }
    }

    public String getAppVersionName() {
        String versionName = "";
        try {
            versionName = getPackageManager().getPackageInfo(getPackageName(), 0).versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return versionName;
    }

    /**
     * 引用资源文件中的字符串
     *
     * @param strId
     * @see [类、类#方法、类#成员]
     */
    @NonNull
    protected String getResuceString(int strId) {
        return getResources().getString(strId);
    }

    /**
     * 延迟1秒关闭activity
     */
    protected void delayFinish() {
        delayFinish(1000);
    }

    /**
     * 延迟指定时间关闭activity
     *
     * @param mills
     */
    protected void delayFinish(int mills) {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                finish();
            }
        }, mills);
    }

    /**
     * 设置Toolbar可否滚动
     *
     * @param scrollAble
     */
    protected void setToolbarScrollAble(boolean scrollAble) {
//        if (toolbar == null) return;
//        int flags = scrollAble ? (AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL
//                | AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS
//                | AppBarLayout.LayoutParams.SCROLL_FLAG_SNAP) : 0;
//        AppBarLayout.LayoutParams layoutParams = (AppBarLayout.LayoutParams) toolbar.getLayoutParams();
//        layoutParams.setScrollFlags(flags);
//        toolbar.setLayoutParams(layoutParams);
    }

    /**
     * 设置透明状态栏
     */
    protected void setTransparentStatusBar() {
        if (Build.VERSION.SDK_INT >= 21) {
            View decorView = getWindow().getDecorView();
            int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            decorView.setSystemUiVisibility(option);
            getWindow().setStatusBarColor(Color.TRANSPARENT);
        }
    }


    @Override
    public void showLoginPage() {
        getActivity().finishAffinity();
        startActivity(new Intent(getActivity(), LoginActivity.class));
    }

    public boolean isAlive() {
        return isAlive;
    }

    //    @AutoAccess
    boolean isFullScreen = false;

    /**
     * 退出全屏模式
     */
    protected void exitFullScreen() {
        showStatusBar();
//        if (toolbar != null) toolbar.setVisibility(View.VISIBLE);
        isFullScreen = false;
    }

    /**
     * 进入全屏模式
     */
    protected void intoFullScreen() {
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
//        if (toolbar != null) toolbar.setVisibility(View.GONE);
        isFullScreen = true;
    }

    private void showStatusBar() {
        final WindowManager.LayoutParams attrs = getWindow().getAttributes();
        attrs.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getWindow().setAttributes(attrs);
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
    }

    @Override
    public void onBackPressed() {
        if (isFullScreen) {
            exitFullScreen();
        } else {
            super.onBackPressed();
        }
    }
}
